package com.coder4j.auth.controller;

import com.coder4j.auth.service.AuthUserService;
import com.coder4j.starter.web.DataScope;
import com.coder4j.auth.entity.AuthUserEntity;
import com.coder4j.auth.vo.AuthUserQuery;
import com.coder4j.auth.vo.AuthUserVO;
import com.coder4j.auth.vo.AuthUserEO;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.jwt.AuthVO;
import com.coder4j.starter.web.util.EasyExcelUtils;
import com.coder4j.starter.tools.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户
 * @author keller
 * @date 2024-05-07 18:10:11
 */
@Slf4j
@RestController
@RequestMapping("/authUser")
public class AuthUserController {
    @Resource
    private AuthUserService service;

    @Resource
    private HttpServletResponse response;

    @PostMapping("/register")
    @ApiNote(name="登录",createName = "keller",createTime = "2024-5-8 16:28:51")
    public R register(@RequestBody AuthUserVO authUserVO){
        if(ObjectUtils.hasEmpty(authUserVO.getAccountNo(),authUserVO.getPassword())){
            return R.paramError();
        }
        AuthVO authVO = service.register(authUserVO.getAccountNo(), authUserVO.getPassword());
        return R.success(authVO);
    }

    @PostMapping("/login")
    @ApiNote(name="登录",createName = "keller",createTime = "2024-5-8 16:28:51")
    public R login(@RequestBody AuthUserVO authUserVO){
        if(ObjectUtils.hasEmpty(authUserVO.getAccountNo(),authUserVO.getPassword())){
            return R.paramError();
        }
        AuthVO authVO = service.login(authUserVO.getAccountNo(), authUserVO.getPassword());
        return R.success(authVO);
    }

    @PostMapping("/logout")
    @ApiNote(name="登出",createName = "keller", createTime = "2024-5-8 17:33:19")
    public R logout(){
       service.logout();
       return R.success();
    }


    /**
     * 添加用户
     * @param authUser
     * @return
     */
    @PostMapping("/add")
    @ApiNote(name="添加用户",createName = "keller",createTime = "2024-5-8 15:33:32")
    public R add(@RequestBody AuthUserVO authUser){
        if(ObjectUtils.hasEmpty(authUser.getAccountNo(),authUser.getUserName())){
            return R.paramError();
        }
        service.addUser(authUser);
        return R.success();
    }

    @PostMapping("/edit")
    @ApiNote(name="修改用户信息",createName = "keller",createTime = "2024-05-07 18:10:11")
    public R edit(@RequestBody AuthUserVO authUser){
        if(ObjectUtils.hasEmpty(authUser.getId())){
            return R.paramError();
        }

        service.editUser(authUser);
        return R.success();
    }

    /**
     * 重置密码
     * @param id
     * @return
     */
    @PostMapping("/resetPasswordById")
    @ApiNote(name="重置密码",createName = "keller",createTime = "2024-05-07 18:10:11")
    public R resetPasswordById( Long id){
        log.info("====== AuthUserControllerDeleteById ======  {}",id);
        service.resetPassword(id);
        return R.success();
    }


    /**
     * 按ID查询用户
     * @param id AuthUser.id
     * @return 用户对象
     */
    @GetMapping("/getById")
    @ApiNote(name="按ID查询用户",createName = "keller",createTime = "2024-05-07 18:10:11")
    public R<AuthUserVO> getById( Long id){

        log.info("====== AuthUserControllerGetById ====== {}",id);
        return R.success(service.baseGetVOById(id));
    }

    /**
     * 查询列表
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
    @DataScope
    @ApiNote(name="查询用户列表",createName = "keller",createTime = "2024-05-07 18:10:11")
    public R<BasePage<AuthUserVO>> getList(@RequestBody AuthUserQuery query){
        if(query == null){
            query = new AuthUserQuery();
        }

        BasePage<AuthUserVO> page = service.baseQueryVOPage(query);
        return R.success(page);
    }

    /**
     * 导出列表
     * @param query 查询条件
     */
    @PostMapping("/export")
    @ApiNote(name="导出用户列表",createName = "keller",createTime = "2024-05-07 18:10:11")
    public void export(@RequestBody AuthUserQuery query){
        if(query == null){
            query = new AuthUserQuery();
        }
        log.info("====== AuthUserControllerGetList ====== {}",query);
        List<AuthUserEntity> list = service.baseQueryList(query);
                List<AuthUserEO> eoList = new ArrayList<>();
                for (AuthUserEntity entity : list) {
                    AuthUserEO eo = new AuthUserEO();
                    BeanUtils.copyProperties(entity,eo);
                    eoList.add(eo);
                }
                EasyExcelUtils.exportClass(response,"AuthUserList",eoList);
    }


    /**
     * 删除用户
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id AuthUser.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name="删除用户",createName = "keller",createTime = "2024-05-07 18:10:11")
    public R deleteById( Long id){
        log.info("====== AuthUserControllerDeleteById ======  {}",id);
        service.baseDeleteById(id);
        return R.success();
    }
}
